package com.company.nuwa.common.utils;

import java.util.Random;

/**
 * <p>RandomCharUtil</p >
 *
 * @author Lionel Lee
 * @version 1.0: RandomCharUtil.java
 * @date 2020/1/3 11:27
 */

public class RandomCharUtil {

    private static final int MIN_LENGTH = 6;

    public static String createRandomCharData(int length) {
        StringBuilder sb = new StringBuilder();

        // 随机用以下三个随机生成器
        Random rand = new Random();
        Random randdata = new Random();
        int data = 0;
        for (int i = 0; i < length; i++) {
            int index = rand.nextInt(3);
            //目的是随机选择生成数字，大小写字母
            switch (index) {
                case 0:
                    //仅仅会生成0~9
                    data = randdata.nextInt(10);
                    while (data == 1 || data == 0) {
                        //仅仅会生成0~9
                        data = randdata.nextInt(10);
                    }
                    sb.append(data);
                    break;
                case 1:
                    //保证只会产生65~90之间的整数
                    data = randdata.nextInt(26) + 65;
                    while (data == 73 || data == 79) {
                        //保证只会产生65~90之间的整数
                        data = randdata.nextInt(26) + 65;
                    }
                    sb.append((char) data);
                    break;
                case 2:
                    //保证只会产生97~122之间的整数
                    data = randdata.nextInt(26) + 97;
                    while (data == 105 || data == 111) {
                        //保证只会产生97~122之间的整数
                        data = randdata.nextInt(26) + 97;
                    }
                    sb.append((char) data);
                    break;
            }
        }
        return sb.toString();
    }

    /**
     * 随机密码
     * 大写字母 + 小写字母 + 数字
     *
     * @param
     * @return
     */
    public static String createRandomCharDataPwd(int length) {

        if (length < MIN_LENGTH) {
            throw new IllegalArgumentException("字符串长度不能小于6");
        }
        //数组，用于存放随机字符
        char[] chArr = new char[length];
        //为了保证必须包含数字、大小写字母
        chArr[0] = (char) ('0' + StdRandom.uniform(0, 10));
        chArr[1] = (char) ('A' + StdRandom.uniform(0, 26));
        chArr[2] = (char) ('a' + StdRandom.uniform(0, 26));

        char[] codes = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
                'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
                'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
                'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
                'y', 'z'};
        //charArr[3..len-1]随机生成codes中的字符
        for (int i = 3; i < length; i++) {
            chArr[i] = codes[StdRandom.uniform(0, codes.length)];
        }

        //将数组chArr随机排序
        for (int i = 0; i < length; i++) {
            int r = i + StdRandom.uniform(length - i);
            char temp = chArr[i];
            chArr[i] = chArr[r];
            chArr[r] = temp;
        }

        ;
        return new String(chArr);
    }


    /***
     * description: 随机生成6位字符串
     * @return java.lang.String
     */
    public static String generate() {

        char[] letters = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
                'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
                'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        boolean[] flags = new boolean[letters.length];
        char[] chs = new char[6];
        for (int i = 0; i < chs.length; i++) {
            int index;
            do {
                index = (int) (Math.random() * (letters.length));
            }
            // 判断生成的字符是否重复
            while (flags[index]);
            chs[i] = letters[index];
            flags[index] = true;
        }
        return String.valueOf(chs);
    }

    public static void main(String[] args) {

        System.out.println("----------");
        System.out.println(RandomCharUtil.createRandomCharDataPwd(8));
        System.out.println(generate());
    }
}
